﻿// CServerDlg.cpp: 实现文件
//

#include "pch.h"
#include "Cobot_PrintServer.h"
#include "CServerDlg.h"

#include "JsonServer.h"

#include "afxdialogex.h"

// CServerDlg 对话框

IMPLEMENT_DYNAMIC(CServerDlg, CDialog)

CServerDlg::CServerDlg(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_DLG_SERVER, pParent)
	, m_strName(_T(""))
	, m_strSex(_T(""))
	, m_strAge(_T(""))
	, m_strMedicineId(_T(""))
	, m_strRecipeId(_T(""))
	, m_strPrintStatus(_T(""))
	, m_strShowInfo(_T(""))
	, m_strRecipeAll(_T(""))
	, m_strRecipeProcessed(_T(""))
	, m_strRecipeWait(_T(""))
{

}

CServerDlg::~CServerDlg()
{
}

void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_IPADDRESS1, m_ctrlIPAddr);
	DDX_Control(pDX, IDC_LIST_CUR_RECIPE, m_CurRecipeListCtrl);
	DDX_Text(pDX, IDC_CUR_NAME, m_strName);
	DDX_Text(pDX, IDC_CUR_SEX, m_strSex);
	DDX_Text(pDX, IDC_CUR_AGE, m_strAge);
	DDX_Text(pDX, IDC_EDIT_MEDICINEID, m_strMedicineId);
	DDX_Text(pDX, IDC_EDIT_PATIENTID, m_strRecipeId);
	DDX_Text(pDX, IDC_STATIC_PRINT_STATUS, m_strPrintStatus);
	DDX_Text(pDX, IDC_EDIT_INFO, m_strShowInfo);
	DDX_Text(pDX, IDC_STATIC_ALL, m_strRecipeAll);
	DDX_Text(pDX, IDC_STATIC_FINISH, m_strRecipeProcessed);
	DDX_Text(pDX, IDC_STATIC_WAIT, m_strRecipeWait);
	DDX_Control(pDX, IDC_STATIC_ALL, m_ctrlAll);
	DDX_Control(pDX, IDC_STATIC_FINISH, m_CtrlFinish);
	DDX_Control(pDX, IDC_STATIC_WAIT, m_CtrlWait);
}


BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
	ON_WM_TIMER()
//	ON_BN_CLICKED(IDC_BTN_SEND, &CServerDlg::OnBnClickedBtnSend)
	ON_MESSAGE(WM_UPDATE_PRINT_STATUS, OnUpdatePrintStatus)
	ON_MESSAGE(WM_UPDATE_RECIPE_NUM, OnUpdateRecipeNum)
	ON_MESSAGE(WM_UPDATE_MEDICINE_BOXID, OnUpdateMedicineBoxId)
	ON_WM_SIZE()
END_MESSAGE_MAP()


// CServerDlg 消息处理程序


void CServerDlg::init()
{
	//初始化界面;
	CImageList m_I;
	m_I.Create(1, 25, TRUE | ILC_COLOR32, 1, 0);
	m_CurRecipeListCtrl.SetImageList(&m_I, LVSIL_SMALL);

	skinppSetNoSkinHwnd(m_ctrlAll.m_hWnd);
	skinppSetNoSkinHwnd(m_CtrlFinish.m_hWnd);
	skinppSetNoSkinHwnd(m_CtrlWait.m_hWnd);

	// 设置处方详情字体.
	CFont font;
	font.CreatePointFont(100, _T("微软雅黑"));
	/*font.CreateFont(100, 100, 0, 0, FW_BLACK, FALSE, FALSE,
		FALSE, GB2312_CHARSET, OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
		FIXED_PITCH | FF_MODERN, _T("宋体"));*/
	m_ctrlAll.SetFont(&font);
	m_CtrlFinish.SetFont(&font);
	m_CtrlWait.SetFont(&font);


	// item 药品名称 规格 数量 用法  货号位   机器发 人工补  
	DWORD dwStyle = m_CurRecipeListCtrl.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;// 选中某行使整行高亮（只适用与report 风格的listctrl ）
	dwStyle |= LVS_EX_GRIDLINES;// 网格线（只适用与report 风格的listctrl ）
	//dwStyle |= LVS_EX_CHECKBOXES;//item 前生成checkbox 控件


	m_CurRecipeListCtrl.SetExtendedStyle(dwStyle);
	
	m_CurRecipeListCtrl.InsertColumn(0, "药品名称", LVCFMT_LEFT, 150);   // 130
	m_CurRecipeListCtrl.InsertColumn(1, "规格",     LVCFMT_LEFT, 105);    //  95
	m_CurRecipeListCtrl.InsertColumn(2, "数量",	    LVCFMT_LEFT, 70);     // 70
	m_CurRecipeListCtrl.InsertColumn(3, "用法",     LVCFMT_LEFT, 120);
	//m_CurRecipeListCtrl.InsertColumn(4, "货号位",   LVCFMT_LEFT, 80);
	m_CurRecipeListCtrl.InsertColumn(4, "机器发",   LVCFMT_LEFT, 80);    // 75
	m_CurRecipeListCtrl.InsertColumn(5, "人工补",   LVCFMT_LEFT, 80);    // 75
}


void CServerDlg::showRecipeInfoList(RecipeDrugInfoSet infoList)
{	
	std::string name = infoList.patientName;
	std::string sex = infoList.patientSex;
	std::string age = infoList.patientAge;
	std::string medicineId = infoList.medicineNum;

	m_strName = name.c_str();
	m_strSex = sex.c_str();
	m_strAge = age.c_str();
	m_strMedicineId = medicineId.c_str();
	m_strRecipeId = infoList.patientId.c_str();

	// 清空表;
	m_CurRecipeListCtrl.DeleteAllItems();

	for (const RecipeDrugInfo& info : infoList.dataSet)
	{	
		int row = m_CurRecipeListCtrl.GetItemCount();
		
		m_CurRecipeListCtrl.InsertItem(row, info.name.c_str());               // 药品名称;
		m_CurRecipeListCtrl.SetItemText(row, 1, info.specification.c_str());  // 药品规格;        
		m_CurRecipeListCtrl.SetItemText(row, 2, info.num.c_str());            // 药品数量;
		m_CurRecipeListCtrl.SetItemText(row, 3, info.instruction.c_str());    // 用法;
		//m_CurRecipeListCtrl.SetItemText(row, 4, info.location.c_str());       // 货号位;
		m_CurRecipeListCtrl.SetItemText(row, 4, info.machineSendNum.c_str()); // 机器发;
		m_CurRecipeListCtrl.SetItemText(row, 5, info.manualSuppleNum.c_str());// 人工补;
	}

	UpdateData(false);
}

void CServerDlg::showServerInfo(std::string info)
{	
	UpdateData(true);
	m_strShowInfo += info.c_str();
	UpdateData(false);
}

void CServerDlg::updatePrinterIP(std::string printIp)
{
	DWORD dwAddress = ntohl(inet_addr(printIp.c_str()));
	m_ctrlIPAddr.SetAddress(dwAddress);
	m_ctrlIPAddr.EnableWindow(false);
}


void CServerDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	// 系统启动，显示当前时间;
	CString strTime;
	CTime tm;
	tm = CTime::GetCurrentTime();
	strTime = tm.Format("%Y-%m-%d %H:%M:%S");
	SetDlgItemText(IDC_STATIC_TIME, strTime);        //显示系统时间

	CDialog::OnTimer(nIDEvent);
}


BOOL CServerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  在此添加额外的初始化

	// 系统启动，显示当前时间;
	CString strTime;
	CTime tm;
	tm = CTime::GetCurrentTime();
	strTime = tm.Format("%Y-%m-%d %H:%M:%S");
	SetDlgItemText(IDC_STATIC_TIME, strTime);        //显示系统时间

	SetTimer(1, 1000, NULL);         //启动定时器

	init();
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CServerDlg::ChangeSize(UINT nID, int x, int y)
{
	CWnd* pWnd;
	pWnd = GetDlgItem(nID);
	if (pWnd != NULL) //判断是否为空，因为在窗口创建的时候也会调用OnSize函数，但是此时各个控件还没有创建，Pwnd为空
	{
		CRect rec;
		pWnd->GetWindowRect(&rec); //获取控件变化前的大小
		ScreenToClient(&rec); //将控件大小装换位在对话框中的区域坐标
		rec.left = rec.left * x / m_rect.Width(); //按照比例调整空间的新位置
		rec.top = rec.top * y / m_rect.Height();
		rec.bottom = rec.bottom * y / m_rect.Height();
		rec.right = rec.right * x / m_rect.Width();
		pWnd->MoveWindow(rec); //伸缩控件
	}
}


//void CServerDlg::OnBnClickedBtnSend()
//{
//	// TODO: 在此添加控件通知处理程序代码
//}

LRESULT CServerDlg::OnUpdatePrintStatus(WPARAM wParam, LPARAM lParam)
{	
	int status = 1;
	status = (int)lParam;
	switch (status)
	{
		case printStatus::Exception:
		{
			m_strPrintStatus = "打印机异常";
			break;
		}
		case printStatus::OK:
		{
			m_strPrintStatus = "一切正常";
			break;
		}
		case printStatus::cashOpen:
		{
			m_strPrintStatus = "有钱箱打开";
			break;
		}
		case printStatus::Offline:
		{
			m_strPrintStatus = "打印机脱机";
			break;
		}
		case printStatus::coverOpen:
		{
			m_strPrintStatus = "上盖打开";
			break;
		}
		case printStatus::Error:
		{
			m_strPrintStatus = "打印机出错";
			break;
		}
		case printStatus::cutterError:
		{
			m_strPrintStatus = "切刀出错";
			break;
		}
		case printStatus::paperNearEnd:
		{
			m_strPrintStatus = "纸将尽";
			break;
		}
		case printStatus::paperEnd:
		{
			m_strPrintStatus = "缺纸";
			break;
		}
	default:
		break;
	}
	
	UpdateData(false);
	return true;
}

LRESULT CServerDlg::OnUpdateRecipeNum(WPARAM wParam, LPARAM lParam)
{	
	SRecipeNumInfo* recipeNmuInfo = (SRecipeNumInfo*)wParam;
	if (recipeNmuInfo != NULL)
	{
		m_strRecipeAll.Format(_T("%d"), recipeNmuInfo->recipeAll);
		m_strRecipeProcessed.Format(_T("%d"), recipeNmuInfo->recipeProcessed);
		m_strRecipeWait.Format(_T("%d"), recipeNmuInfo->recipePending);
		
		UpdateData(false);

		delete recipeNmuInfo;
		recipeNmuInfo = NULL;
	}
	
	return true;
}

LRESULT CServerDlg::OnUpdateMedicineBoxId(WPARAM wParam, LPARAM lParam)
{	
	int id = int(lParam);
	CString medicineBoxId;
	medicineBoxId.Format("%d",id);
	m_strRecipeId = medicineBoxId;

	UpdateData(false);

	return true;
}


BOOL CServerDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (pMsg->message == WM_KEYDOWN)
	{
		switch (pMsg->wParam)
		{
		case VK_RETURN: // 屏蔽回车
			return TRUE;
		case VK_ESCAPE:
			return TRUE;
		}
	}

	return CDialog::PreTranslateMessage(pMsg);
}


void CServerDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
	if (nType != SIZE_MINIMIZED) //判断窗口是不是最小化了，因为窗口最小化之后 ，窗口的长和宽会变成0，当前一次变化的时就会出现除以0的错误操作
	{
		// IDC_STATIC;
		ChangeSize(IDC_GROUP_RECIPE_INFO, cx, cy); //对每一个控件依次做调整

		ChangeSize(IDC_STATIC_ALL_TEXT, cx, cy);
		ChangeSize(IDC_STATIC_ALL, cx, cy);

		ChangeSize(IDC_STATIC_FINISH_TEXT, cx, cy);
		ChangeSize(IDC_STATIC_FINISH, cx, cy);

		ChangeSize(IDC_STATIC_WAIT_TEXT, cx, cy);
		ChangeSize(IDC_STATIC_WAIT, cx, cy);

		ChangeSize(IDC_GROUP_PATIENT_INFO, cx, cy);
		ChangeSize(IDC_STATIC_PATIENTID_TEXT, cx, cy);
		ChangeSize(IDC_EDIT_PATIENTID, cx, cy);

		ChangeSize(IDC_EDIT_MEDICINEID_TEXT, cx, cy);
		ChangeSize(IDC_EDIT_MEDICINEID, cx, cy);

		ChangeSize(IDC_GROUP_CURRECIPE, cx, cy);  // 
		ChangeSize(IDC_NAME_TEXT, cx, cy);
		ChangeSize(IDC_SEX_TEXT, cx, cy);
		ChangeSize(IDC_AGE_TEXT, cx, cy);

		ChangeSize(IDC_CUR_NAME, cx, cy);
		ChangeSize(IDC_CUR_SEX, cx, cy);
		ChangeSize(IDC_CUR_AGE, cx, cy);
		ChangeSize(IDC_LIST_CUR_RECIPE, cx, cy); 
		ChangeSize(IDC_EDIT_INFO, cx, cy);

		ChangeSize(IDC_STATIC_TIME_TEXT, cx, cy);
		ChangeSize(IDC_STATIC_STATUS_TEXT, cx, cy);
		ChangeSize(IDC_STATIC_IP_TEXT, cx, cy);

		ChangeSize(IDC_IPADDRESS1, cx, cy);
		ChangeSize(IDC_STATIC_PRINT_STATUS, cx, cy);
		ChangeSize(IDC_STATIC_TIME, cx, cy);

		ChangeSize(IDC_GROUP_STATUS, cx, cy);

		GetClientRect(&m_rect); //最后要更新对话框的大小，当做下一次变化的旧坐标
	}
}
